<?php

namespace Goods\Controller;
use Admin\Common\CommonController;

class PucController extends CommonController
{

    /**
     * 发布商品
     */
     public function add(){
         if(IS_POST){
//             dd($_POST);die;
           if(notNull(I('post.gname'))){
              $this->error('请填写商品名称');die;
           }  if(notNull(I('post.price'))){
              $this->error('请填写商品价格');die;
           }  if(notNull(I('post.nid'))){
              $this->error('请选择商品种类');die;
           }  if(notNull(I('post.cid'))){
              $this->error('请选择商品分类');die;
           }  if(notNull(I('post.bid'))){
              $this->error('请请选择商品品牌');die;
           }  if(notNull(I('post.style'))){
              $this->error('请填写属性');die;
           }  if(notNull(I('post.thumb'))){
              $this->error('请上传商品代表图');die;
           }
//             处理商品属性
             $style=$this->style(I('post.style'));
             $style=json_encode($style,JSON_UNESCAPED_UNICODE);
             $data['gname']=$_POST['gname'];
             $data['number']=$_POST['number'];
             $data['price']=$_POST['price'];
             $data['priced']=$_POST['priced'];
             $data['nid']=$_POST['nid'];
             $data['cid']=$_POST['cid'];
             $data['bid']=$_POST['bid'];
             $data['digest']=$_POST['digest'];
             $data['style']=$style;
             $data['styled']=$_POST['style'];
             $data['adduser']=$_POST['adduser'];
             $data['content']=$_POST['editorValue'];
             $data['sort']=$_POST['sort'];
             $data['thumb']=$_POST['thumb'];
             $data['addtime']=time();
             $res=D('goods')->add($data);
             if($res['valid']){
                 $pic['gid']=$res['valid'];
                 $pic['pic']=I('pic');
                 $res1=D('pic')->add($pic);
                 if($res1){
                     $this->success('发布成功',U('goods/lists/index'));die;
                 }else{
                     $this->error('发布失败');die;
                 }
             }else{
                $this->jump($res);
             }
         }
         //找出所有的种累信息
         $nature=M('nature')->select();
//         dd($nature);
         $this->assign('nature',$nature);
         //找出所有的分类信息
         $category=M('category')->select();
         $this->assign('category',$category);
//         dd($category);
         //展出品牌信息
         $brand=M('brand')->select();
         $this->assign('brand',$brand);
        $this->display();
     }

    //处理商品属性
    public function style($style){
        //用正则拆分字符串
        $style=preg_split('/(\n|\r)/',$style);
       //过滤空数组
        $style=array_filter($style);
        //拆成数组
        $mar=[];
        foreach($style as $k=>$v){
           $mar[]=explode('|',$v);
        }
        //拼成属性=>属性值
        $res=[];
        foreach($mar as $k=>$v){
            $res[][$v[0]]=$v[1];
        }
        return $res;
    }

    /**
     * 缩略图上传
     */
    public  function pic(){
//        dd($_FILES);die;
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     'goods/'; // 设置附件上传（子）目录
        // 上传单个文件
        $info   =   $upload->uploadOne($_FILES['Filedata']);
//        dd($info);
        if(!$info) {// 上传错误提示错误信息
            dd($upload->getError());
        }else{// 上传成功 获取上传文件信息
//            system/2017-03-17/58cb8b6f06155.png
            echo __ROOT__.'/Uploads/'.$info['savepath'].$info['savename'];
        }
    }

    //删除
    public  function  delimg(){
        $path=$_POST['path'];
        M('pic')->where("pic='$path'")->delete();
        $path=substr($path,5);
        $path='.'.$path;
        if(file_exists($path)){
            $res=unlink($path);
        }
        echo 1;
    }


}